﻿using UtilsORM;
using UtilsORM.MySqlData;

namespace GatewayServerService.Data
{
    public class DBIotDeviceLinkage
    {


        public List<iot_device_linkage> getLinkageList(string cguid, int status)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            return ef.iot_device_linkage.Where(c => c.l_c_guid == cguid && c.l_status == status).ToList();

        }
        public iot_device_linkage getLinkage(string guid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            return ef.iot_device_linkage.Where(c => c.l_guid == guid).FirstOrDefault();

        }


        public bool saveAll(MagerLoginInfo LoginUserInfo, iot_device_linkage req)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            //先查一下有没重复提交
            iot_device_linkage model = ef.iot_device_linkage.Where(c => c.l_guid == req.l_guid).FirstOrDefault();
            if (model == null)
            {

                //写入
                req.l_c_guid = LoginUserInfo.userCompanyGuid;
                req.l_cu_guid = LoginUserInfo.user_guid;
                req.l_mod_time = req.l_add_time = DateTime.Now;
                ef.iot_device_linkage.Add(req);
                ef.SaveChanges();
            }
            else
            {

                model.l_title = req.l_title;
                model.l_condition_type = req.l_condition_type;
                model.l_mod_time = req.l_add_time = DateTime.Now;
                ef.iot_device_linkage.Update(model);
                ef.SaveChanges();
            }

            return true;
        }

    }
}
